import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_yres/tool/Toast.dart';
import 'package:flutter_yres/tool/BlankToolBarTool.dart';

class LoginPwdPage extends StatefulWidget {
  LoginPwdPage({Key key}) : super(key: key);

  @override
  _LoginPwdPageState createState() => _LoginPwdPageState();
}

class _LoginPwdPageState extends State<LoginPwdPage> {
  final _oldPwdController = TextEditingController(); //用户名
  final _userPwdController = TextEditingController(); //密码
  final _userPwdController2 = TextEditingController(); //密码
  // 响应空白处的焦点的Node
  BlankToolBarModel barModel = BlankToolBarModel();
  ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('修改密码'),
        ),
        body: BlankToolBarTool.blankToolBarWidget(context,
            model: barModel,
            body: Container(
                margin: EdgeInsets.fromLTRB(0, kWindowstateHeight, 0, 0),
                height: ScreenUtil.getInstance().setHeight(1334) - kWindowstateHeight - 56,
                child: SingleChildScrollView(
                    controller: scrollController,
                    child: Column(
                      children: <Widget>[
                        Container(
                            child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                              createInputText(_oldPwdController,
                                  hint: '请输入原密码',
                                  text: '原密码',
                                  obscureText: false,
                                  type: TextInputType.phone),
                              createInputText(_userPwdController,
                                  hint: '请输入密码',
                                  text: '新密码',
                                  obscureText: true,
                                  type: TextInputType.visiblePassword),
                              createInputText(_userPwdController2,
                                  hint: '请再次输入密码',
                                  text: '确认新密码',
                                  obscureText: true,
                                  type: TextInputType.visiblePassword),
                              new Container(
                                  margin: EdgeInsets.fromLTRB(
                                      0.0, ScreenUtil.getInstance().setWidth(40), 0, 0.0),
                                  width: ScreenUtil.getInstance().setWidth(750) * 0.8,
                                  child: new RaisedButton(
                                      color: Colors.blue,
                                      shape: StadiumBorder(),
                                      child: new Padding(
                                        padding: new EdgeInsets.all(10.0),
                                        child: new Text(
                                          '保存',
                                          style: new TextStyle(
                                              color: Colors.white,
                                              fontSize: 16.0),
                                        ),
                                      ),
                                      onPressed: () {
                                        _getLoginMsg();
                                      })),
                            ])),
                      ],
                    )))));
  }

  //文字 + 输���框
  Widget createInputText(TextEditingController controller,
      {obscureText: false, String hint, String text, TextInputType type}) {
    FocusNode focusNode = barModel.getFocusNodeByController(controller);
    return Padding(
        padding:
            new EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(40), ScreenUtil.getInstance().setWidth(20), ScreenUtil.getInstance().setWidth(40), 0),
        child: new Stack(
          alignment: new Alignment(1.0, 1.0),
          //statck
          children: <Widget>[
            new Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  new Container(
                    alignment: Alignment.centerLeft,
                    padding: new EdgeInsets.fromLTRB(0.0, 0.0, ScreenUtil.getInstance().setWidth(5), 0.0),
                    child: new Text(
                    text
                    ),
                  ),
                  Expanded(
                    child: new Container(
                    // width: kScreenWidth * 0.55,
                    child: new TextField(
                        // focusNode: _contentFocusNode,
                        controller: controller,
                        keyboardType: type,
                        decoration: new InputDecoration(
                          border: InputBorder.none,
                          hintText: hint,
                        ),
                        obscureText: obscureText,
                        focusNode: focusNode),
                  )
                  )
                 ,
                  Container(
                    margin: new EdgeInsets.fromLTRB(0.0, 0.0, ScreenUtil.getInstance().setWidth(8), 0.0),
                    width: ScreenUtil.getInstance().setWidth(40),
                    height: ScreenUtil.getInstance().setWidth(40),
                    child: new IconButton(
                      icon: new Icon(Icons.clear, color: Colors.black45),
                      onPressed: () {
                        controller.clear();
                      },
                    ),
                  )
                ]),
            Divider(
              height: 1,
            ),
          ],
        ));
  }

  void _getLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    var token = preferences.get("token");
    if (token == null || token == '') {
      //未登陆
      Navigator.of(context).pushNamed("login");
    } else {
      _requestForget(token);
    }
  }

  // 保存密码
  void _saveLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString("pwd", _userPwdController.text);
  }

  _requestForget(token) async {
    if (_oldPwdController.text == '' ||
        _userPwdController.text == '' ||
        _userPwdController2.text == '') {
      Toast.toast(context, msg: '填写完整信息');
      return;
    }
    if (_userPwdController2.text != _userPwdController.text) {
      Toast.toast(context, msg: '两次密码不一致');
      return;
    }
    // FocusScope.of(context).requestFocus();
    FormData formData = new FormData.fromMap({
      'token': token,
      "oldpwd": _oldPwdController.text,
      "pwd1": _userPwdController.text,
      "pwd2": _userPwdController2.text
    });
    print(formData);
    Response<String> response =
        await HttpUtil().post(Api.ChangePwdUrl, data: formData);
    Map userMap = json.decode(response.data);
    print(userMap);
    Toast.toast(context, msg: userMap['msg']);
    int code = userMap['code'];
    if (code == 1) {
      _saveLoginMsg();
      // Navigator.pop(context, '');
    } else {}
  }
}
